package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2004
 *
 * Contains information for absences and tardies.
 */
public class AttendanceContainer extends Container {

    // The map of timer buckets for all databases
    protected static Hashtable dbStores = new Hashtable();
    // The auto-generated ID of the student for whom this attendance applies.
    private StringItem studentID =
        new StringItem(SchoolDBSQL.USRASSIGNEDID, "", true);

    // The auto-generated ID of the class in which the homework was assigned.
    private StringItem classID =
        new StringItem(SchoolDBSQL.CLASSASSIGNEDID, "", true);

    // The date absent or tardy.
    private DateNoTimeItem date =
        new DateNoTimeItem(SchoolDBSQL.ATNDDATE, "", true);

    // The comment associated with the attendance. No validation is done.
    private StringItem type =
        new StringItem(SchoolDBSQL.ATNDTYPE, "", true);

    /**
     * Default constructor.
     */
    private AttendanceContainer() {

    }

    /**
     * Constructor creates a new container filled with values loaded from the database using
     * the class ID, homework ID, and student ID
     * as keys.
     * @param sID The auto-generated student ID.
     * @param cID The auto-generated class ID.
     * @param newDate The date of attendance. This should be formatted in the mm/dd/yyyy format.
     * @dbName The database of the school where the query should be performed.
     */
    private AttendanceContainer(String sID, String cID, String newDate, String dbName) {
        getStudentID().setValue(sID);
        getClassID().setValue(cID);
		getDate().setValue(newDate);
        SchoolDBSQL.getSchoolDBSQL(dbName).loadAttendance(
            getValidationItems(),
            new Item[] { getStudentID() , getClassID(), getDate()});
    }

    public static AttendanceContainer getAttendanceContainer(String sID, String cID, String date, String dbName) {
        AttendanceContainer returnVal = null;
        DBStore dbs = null;

        if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
            dbs = new DBStore();
            dbStores.put(dbName, dbs);
            dbs.start();
        }

        if ((returnVal = (AttendanceContainer) dbs.getContainer("S" + sID + "C" + cID + "D" + date)) == null) {
            returnVal = new AttendanceContainer();
            returnVal.getStudentID().setValue(sID);
            returnVal.getClassID().setValue(cID);
            returnVal.getDate().setValue(date);
            SchoolDBSQL.getSchoolDBSQL(dbName).loadAttendance(returnVal.getValidationItems(),
                                                         new Item[] {returnVal.getStudentID(),
                                                                     returnVal.getClassID(),
                                                                     returnVal.getDate()});
            dbs.addContainer(returnVal, "S" + sID + "C" + cID + "D" + date);
        }

        return returnVal;
    }

    /* (non-Javadoc)
     * @see schoolsource.model.Container#getValidationItems()
     */
    public Item[] getValidationItems() {
        ArrayList al = new ArrayList();
        al.add(studentID);
        al.add(classID);
        al.add(date);
        al.add(type);

        return (Item[]) al.toArray(new Item[al.size()]);
    }

    /**
     * Getter for the student ID for which this attendance applies.
     * @return The auto-generated ID of the student for which this attendance applies.
     */
    public StringItem getStudentID() {
        return studentID;
    }

    /**
     * Getter for the class ID of the attendance.
     * @return The auto-generated ID of the class for which this attendance applies.
     */
    public StringItem getClassID() {
        return classID;
    }

    /**
     * Getter for the date.
     * @return The date.
     */
    public DateNoTimeItem getDate() {
        return date;
    }

    /**
     * Getter for whether it was an absence or a tardy.
     * @return The type.
     */
    public StringItem getType() {
        return type;
    }

    /**
     * Prints the values of the container.
     */
    public void printItems() {
        SchoolLogger.logger.log(
            Level.INFO,
            "************************************************");
        Item iList[] = getValidationItems();
        for (int j = 0; j < iList.length; j++) {
            if (iList[j] != null) {
                SchoolLogger.logger.log(
                    Level.INFO,
                    iList[j].getFieldName() + ": " + iList[j].toString());
            }
        }
    }
}
